package com.tdam2011.grupo02.support;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.tdam2011.grupo02.domain.ContactLocation;
import com.tdam2011.grupo02.domain.MensajeWeb;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class MapLocationController {

	private static Context context;
	private LocationManager locationManager;
	private LocationListener locationListener;
	private Location location;
	private LocationUpdateInterface locationUpdate;

	public static final String TAG = "LocationDemo";

	public MapLocationController(Context context,
			LocationUpdateInterface locationUpdate) {
		this.context = context;
		this.locationUpdate = locationUpdate;
		initLocationManager();
	}

	private void initLocationManager() {
		if (locationManager == null) {
			locationManager = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
			locationListener = new LocationListener() {

				@Override
				public void onLocationChanged(Location location) {
					Log.d(TAG, "new location: " + location.getLatitude()
							+ " - " + location.getLongitude());
					locationUpdate.updateLocation(location);
				}

				@Override
				public void onProviderDisabled(String provider) {
				}

				@Override
				public void onProviderEnabled(String provider) {
				}

				@Override
				public void onStatusChanged(String provider, int status,
						Bundle extras) {
				}

			};
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		}
	}

	public double getLongitude() {
		if (location != null)
			return location.getLongitude();
		return -1;
	}

	public double getLatitud() {
		if (location != null)
			return location.getLatitude();
		return -1;
	}

	public GeoPoint getGeoPoint(Location location) {
		GeoPoint point = null;
		if (location != null) {
			Double latitude = location.getLatitude() * 1E6;
			Double longitude = location.getLongitude() * 1E6;

			point = new GeoPoint(latitude.intValue(), longitude.intValue());
		}

		return point;
	}

	public static ContactLocation parseLocation(Context context,
			String dataToParse) {
		ContactLocation location = null;
		if (dataToParse != null) {
			String[] aux = dataToParse.split(MensajeWeb.LOCATION_TAG);
			String locationData = "";

			if (aux != null && aux.length != 1) {
				locationData = aux[aux.length - 1];
				int indexSlash = locationData
						.indexOf(MensajeWeb.LOCATION_TAG_SLASH);
				int indexDot = locationData.indexOf(MensajeWeb.LOCATION_TAG_DOT);

				String latitude = locationData.substring(0, indexSlash);
				String longitude = locationData.substring(indexSlash + 1,
						indexDot);
				String address = locationData.substring(indexDot + 1,
						locationData.length());

				Log.d("PARSE_LOCATION_DEMO", locationData);
				Log.d("PARSE_LOCATION_DEMO", latitude);
				Log.d("PARSE_LOCATION_DEMO", longitude);				
				Log.d("PARSE_LOCATION_DEMO", address);

				location = new ContactLocation();
				location.setLatitud(Double.parseDouble(latitude));
				location.setLongitude(Double.parseDouble(longitude));
				location.setAddress(address);
			}
		}
		return location;
	}

	public static String parseMessage(String dataToParse) {
		String message = null;
		if (dataToParse != null) {
			String[] aux = dataToParse.split(MensajeWeb.LOCATION_TAG);

			if (aux != null && aux.length != 0) {
				message = aux[0];
			} else
				message = dataToParse;
		}
		return message;
	}

	public static String getContactLocationAddress(Context context,
			double latitud, double longitude) {
		Geocoder gc = new Geocoder(context, Locale.getDefault());
		List<Address> addresses = new LinkedList<Address>();
		StringBuilder sb = new StringBuilder();

		try {
			addresses = gc.getFromLocation(latitud, longitude, 1);
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (addresses.size() > 0) {
			Address address = addresses.get(0);

			sb.append(address.getAddressLine(0)).append("\n");
			sb.append(address.getLocality()).append(" - ");
			sb.append(address.getCountryCode());
		}

		return sb.toString();
	}

	public void disableLocationManager() {
		if (locationListener != null)
			locationManager.removeUpdates(locationListener);
	}
}
